﻿using System;
using System.Collections.Generic;
using System.Threading;
using CommonLibrary.Models.ControllerStates.PS3;

namespace InputManager.Helpers
{
    public static class KeyToPS3Struct
    {
        private static readonly Mutex MutAxis = new Mutex();
        private static readonly Mutex MutKeys = new Mutex();

        private const ushort mideValue = 32768;

        public static void MouseMovementToPS3CS(this PS3ControllerState state, string actionName, ushort value)
        {
            MutAxis.WaitOne();

            switch (actionName)
            {
                case "LeftX":
                    state.User.LAxisX = value;
                    break;
                case "LeftY":
                    state.User.LAxisY = value;
                    break;
                case "RightX":
                    state.User.RAxisX = value;
                    break;
                case "RightY":
                    state.User.RAxisY = value;
                    break;
            }

            MutAxis.ReleaseMutex();
        }

        public static void KeyCharToPS3CS(this PS3ControllerState state, string action, bool isPressed)
        {
            MutKeys.WaitOne();
            switch (action)
            {
                case "Select":
                    state.User.Buttons.Select.Pressed = isPressed;
                    state.User.Buttons.Select.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Start":
                    state.User.Buttons.Start.Pressed = isPressed;
                    state.User.Buttons.Start.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "PS":
                    state.User.Buttons.PS.Pressed = isPressed;
                    state.User.Buttons.PS.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;

                case "Triangle":
                    state.User.Buttons.Triangle.Pressed = isPressed;
                    state.User.Buttons.Triangle.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Square":
                    state.User.Buttons.Square.Pressed = isPressed;
                    state.User.Buttons.Square.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Cross":
                    state.User.Buttons.Cross.Pressed = isPressed;
                    state.User.Buttons.Cross.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Circle":
                    state.User.Buttons.Circle.Pressed = isPressed;
                    state.User.Buttons.Circle.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;

                case "Left":
                    state.User.Buttons.Left.Pressed = isPressed;
                    state.User.Buttons.Left.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Right":
                    state.User.Buttons.Right.Pressed = isPressed;
                    state.User.Buttons.Right.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Up":
                    state.User.Buttons.Up.Pressed = isPressed;
                    state.User.Buttons.Up.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "Down":
                    state.User.Buttons.Down.Pressed = isPressed;
                    state.User.Buttons.Down.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;

                case "L1":
                    state.User.Buttons.L1.Pressed = isPressed;
                    state.User.Buttons.L1.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "R1":
                    state.User.Buttons.R1.Pressed = isPressed;
                    state.User.Buttons.R1.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "L2":
                    state.User.Buttons.L2.Pressed = isPressed;
                    state.User.Buttons.L2.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "R2":
                    state.User.Buttons.R2.Pressed = isPressed;
                    state.User.Buttons.R2.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "L3":
                    state.User.Buttons.L3.Pressed = isPressed;
                    state.User.Buttons.L3.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;
                case "R3":
                    state.User.Buttons.R3.Pressed = isPressed;
                    state.User.Buttons.R3.Value = isPressed ? byte.MaxValue : byte.MinValue;
                    break;

                case "LeftX+":
                    state.User.LAxisX = isPressed ? UInt16.MaxValue : mideValue;
                    break;
                case "LeftX-":
                    state.User.LAxisX = isPressed ? UInt16.MinValue : mideValue;
                    break;
                case "LeftY+":
                    state.User.LAxisY = isPressed ? UInt16.MinValue : mideValue;
                    break;
                case "LeftY-":
                    state.User.LAxisY = isPressed ? UInt16.MaxValue : mideValue;
                    break;
                case "RightX+":
                    state.User.RAxisX = isPressed ? UInt16.MaxValue : mideValue;
                    break;
                case "RightX-":
                    state.User.RAxisX = isPressed ? UInt16.MinValue : mideValue;
                    break;
                case "RightY+":
                    state.User.RAxisY = isPressed ? UInt16.MinValue : mideValue;
                    break;
                case "RightY-":
                    state.User.RAxisY = isPressed ? UInt16.MaxValue : mideValue;
                    break;
            }
            MutKeys.ReleaseMutex();
        }
    }
}
